#!/usr/bin/perl
#setpass-lib.pl
#Library for editing webmin users, password and access rights

do '../web-lib.pl';

&init_config();
%access = &get_module_acl();

#&foreign_require("acl", acl-lib.pl);

#check_passwd(username, password)
sub check_passwd
{
#	print("<p> $aa \n");
	return &validate_user($_[0], $_[1]);
}

sub validate_user
{
	return 0 if (!$_[0]);

	%webminusers = ();
	$openerror = 0;

	#$openerror = open (WEBMINUSERS, "/etc/webmin/miniserv.users");
	$openerror = open (WEBMINUSERS, $config{"config_miniserv_user"});
	if (!$openerror)
	{
		return 0;
	}

	while($line = <WEBMINUSERS>)	#create a webminusers table include username and password
	{
		if ($line =~ /((.*):(.*):(.*):(.*):(.*))/)
		{
			$webminusers{"$2"} = $3;
		}
	}

	if (!$webminusers{$_[0]})
	{
		# See if this user exists in Unix and can be validated by the same
		# method as the unixauth webmin user
		print("<p> $_[0] is not webminusers mey be a unix user.\n");
		return 0;
	}
	elsif ($webminusers{$_[0]} eq 'x')
	{
		# Call PAM to validate the user
		print("<p> $_[0] is unix user. not a webminuser.\n");
		return 0;
	}
	else
	{
		# Check against the webmin user list
		return $webminusers{$_[0]} eq crypt($_[1], $webminusers{$_[0]}) ? 1 : 0;
	}
}


#modify_user(username, @detail)
#@detail=(username, passwd, sync, cert, allow_deny)
sub modify_user
{
	foreign_require("acl", "acl-lib.pl");
	&foreign_call("acl", "modify_user", $_[0], $_[1]);
}

#create a new cryptpasswd
#crt_cryptpasswd(password)
#return new cryptpasswd
sub crt_cryptpasswd
{
	local($newcryptpasswd, $salt);
	$salt = chr(int(rand(26) + 65)).chr(int(rand(26) + 65));
	$newcryptpasswd = crypt($_[0], $salt);
	return $newcryptpasswd;
}

#save webmin module.acl file confirm the user could access modules
#save_webmin_acl($remote_user, new_user,\@module);
sub save_webmin_acl
{
	my ($username) = @_[0];
	my ($newusername) = @_[1];
	my ($module) = @_[2];
	my ($linenum) = undef;

	#&lock_file("/etc/webmin/webmin.acl");
	#$lref = &read_file_lines("/etc/webmin/webmin.acl");
	&lock_file($config{"config_webmin_acl"});
	$lref = &read_file_lines($config{"config_webmin_acl"});

	#print $username;
	#print $newusername;

	#$linemodule = join(" ", @$module);
	#print $linemodule;

	$linenum = 0;	#look for the line to change in file
	while($linenum < @$lref)
	{
		if($lref->[$linenum] =~ /^(\s*)($username):/)
		{
			$linemodule = join(" ", @$module);
			$lref->[$linenum] = $newusername.": ".$linemodule;
			#print $linemodule;
			last;
		}
		$linenum++;
	}
	&flush_file_lines();
	#&unlock_file("/etc/webmin/webmin.acl");
	&unlock_file($config{"config_webmin_acl"});
}

#get_webmin_user_acl($username)
sub get_webmin_user_acl()
{
	@rt = ();
	$username = @_[0];

	$openerror = open(WEBMINACL, $config{"config_webmin_acl"});
	while($line = <WEBMINACL>)
	{
		if ($line =~ /$username:(.*)/)
		{
			$line = $1;
			if(!defined(@rt))
			{
				chomp($line);
				#print $line;
				@rt = split(/ +/, $line);
			}
			else
			{
				chomp($line);
				#print $line;
				@rt = (@rt, split(/ +/ ,$line));
			}
		}
	}
	close(DEFAULTACL);

	return @rt;
}

#sub check_simbol_uname($inputname)
#return $error_msg
sub check_simbol_uname()
{
	my ($inputname, $error_msg);

	$inputname = @_[0];
	$error_msg = "";

	if(($inputname =~ /[^a-zA-Z0-9]/) || ($inputname =~ /^\d+(.*)/))
	{
		$error_msg = $text{'error_msg_isnt_simbol'};
	}

	return $error_msg;
}

#is_exist_webmin_user($username)
#if exist then return $username
#else return "";
sub is_exist_webmin_user()
{
	my ($username, $i, @wbusrfile, @line);

	$username = @_[0];
	open(WBUSR, $config{"config_miniserv_user"});
	@wbusrfile = <WBUSR>;
	close(WBUSR);

	for($i = 0; $i < @wbusrfile; $i++)
	{
		chomp($wbusrfile[$i]);
		$wbusrfile[$i] =~ s/#.*$//;
		@line = split(":", $wbusrfile[$i]);
		if($line[0] eq $username)
		{			
			return $username;
			last;
		}
	}
	return "";
}

1;
